home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 85 < prev    next >
Internet Message Format  |  1996-08-06  |  4KB

  1. Path: lyra.csx.cam.ac.uk!nmm1
  2. From: nmm1@cus.cam.ac.uk (Nick Maclaren)
  3. Newsgroups: comp.std.c,comp.arch.arithmetic,sci.math.num-analysis
  4. Subject: Re: Why is <float.h>? [was Re: sizeof(1L) in preprocesor ...]
  5. Date: 12 Jan 1996 10:36:58 GMT
  6. Organization: University of Cambridge, England
  7. Distribution: inet
  8. Message-ID: <4d5dka$iua@lyra.csx.cam.ac.uk>
  9. References: <sc3f9vb6gk.fsf@lns101.lns.cornell.edu> <4ctk66$57j@lyra.csx.cam.ac.uk> <KANZE.96Jan9135752@slsvewt.lts.sel.alcatel.de> <4ctpkv$82v@lyra.csx.cam.ac.uk> <4d1hh7$kd@lyra.csx.cam.ac.uk> <4d4fgb$22d@usenet.pa.dec.com>
  10. NNTP-Posting-Host: ursa.cus.cam.ac.uk
  11.  
  12. In article <4d4fgb$22d@usenet.pa.dec.com>, diamond@tko.dec.com (Norman Diamond) writes:
  13. |> In article <4d1hh7$kd@lyra.csx.cam.ac.uk>, nmm1@cus.cam.ac.uk (Nick Maclaren) writes:
  14. |> >Numerical analysts use values of the sort that are found in <float.h>
  15. |> >for many purposes, including:
  16. |> >    1) Switching to alternate algorithms for special functions.
  17. |> >    2) Avoiding overflow, underflow and rounding error.
  18. |> >    3) Selecting appropriate precisions for conversion on I/O.
  19. |> >    4) Varying the algorithm in the rare cases where it matters.
  20. |> >Now, (1) needs the values to be preprocessor constants for efficiency.
  21. |> >Remember that such people are the very people who write the functions
  22. |> >called via <math.h>, and we like to be able to write code that compiles
  23. |> >automatically and correctly even on machines that we have never heard
  24. |> >of!  But to do this efficiently, we have to define our OWN <float.h>,
  25. |> >which isn't portable!
  26. |> 
  27. |> No, for (1), you do not need to define your own <float.h>.  You need
  28. |> to address two categories:
  29. |> 
  30. |> (A)  Implementations where definitions in <float.h> are preprocessor
  31. |> constants.  You can write one version of your code using #if directives
  32. |> as necessary, presuming that these are preprocessor constants, and your
  33. |> code will work under all such implementations.
  34. |> 
  35. |> (B)  Implementations where definitions in <float.h> are not preprocessor
  36. |> constants.  ....  You can write one version of
  37. |> your code using if statements as necessary, without presuming that these
  38. |> are preprocessor constants, and your code will work under all implementations.
  39. |> 
  40. |> You might want to provide just one version with additional #if directives
  41. |> to choose between version (A) for efficiency and (B) for reliability.
  42.  
  43. I am afraid not.  Here is a sample of the problems:
  44.  
  45.     1) You are proposing writing two separate libraries, which is
  46. 3-4 times the work of writing one, assuming that you want to keep
  47. them 100% compatible.
  48.     2) Many versions of <float.h> have SOME values as pre-processor
  49. constants and others as general rvalues (e.g. Solaris 2).
  50.     3) You have to allow for the case where the values are constants,
  51. but not pre-processor constants.
  52.     4) There is no portable way to test whether (A) or (B) is true,
  53. let alone whether you have an intermediate case.
  54.  
  55. Remember that the semantic rules for the evaluation of preprocessor
  56. expressions are not the same as those for compile-time constant
  57. expressions, which are not the same as those for run-time ones.
  58.  
  59. Also, remember that I am into REAL portability - i.e. I write code
  60. that is expected to work on hardware and under an operating system
  61. and compiler that I had never heard of when I was writing the code.
  62. And I don't just mean UNIX-like ones.
  63.  
  64. My point was never that it cannot be done - I have been doing it
  65. for 25 years.  My point is that <float.h> is almost useless for
  66. much or most numerical code.
  67.  
  68.  
  69. Nick Maclaren,
  70. University of Cambridge Computer Laboratory,
  71. New Museums Site, Pembroke Street, Cambridge CB2 3QG, England.
  72. Email:  nmm1@cam.ac.uk
  73. Tel.:  +44 1223 334761    Fax:  +44 1223 334679
  74.  
  75.  
  76.